package demo.thread;

public class ThreadDemo {
    public static void main(String[] args) {
        System.out.println("当前线程：" + Thread.currentThread().getName());
        System.out.println("==============================");

        // threadTest();
        joinTest();
    }

    private static void threadTest() {
        // 创建MyRunnable类
        MyRunnable mr = new MyRunnable();
        // 创建Thread类的有参构造,并设置线程名
        Thread t1 = new Thread(mr, "张飞");
        Thread t2 = new Thread(mr, "貂蝉");
        Thread t3 = new Thread(mr, "吕布");
        // 启动线程
        t1.start();
        t2.start();
        t3.start();
    }

    /**
     * join()方法测试
     */
    private static void joinTest() {
        // 创建MyRunnable类
        MyRunnable mr = new MyRunnable();
        Thread t1 = new Thread(mr, "令狐冲");
        Thread t2 = new Thread(mr, "任盈盈");
        Thread t3 = new Thread(mr, "岳不群");

        // 启动线程
        t1.start();
        try {
            // 等待t1执行完才会轮到t2，t3抢
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        t3.start();
    }
}
